//
//  SetUpMoneyPasswordViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/11.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "SetUpMoneyPasswordViewController.h"
#import "AFNetworking.h"

@interface SetUpMoneyPasswordViewController ()<UITextFieldDelegate>

@end

@implementation SetUpMoneyPasswordViewController

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"User" forKey:@"Model"];
    [parametersDic setObject:@"BankPWD_Edit" forKey:@"Action"];
   NSString *NSUTF8 = [NSString stringWithFormat:@"U_PassWord=%@&U_PassWord1=%@&U_PassWord2=%@",_setUpMoneyPasswordTextField.text,_setUpMoneyPasswordTextField2.text,_setUpMoneyPasswordTextField3.text];
    NSUTF8 = [NSUTF8 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [parametersDic setValue:NSUTF8 forKey:@"Data"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
//    NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil]);
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
    UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:[dic objectForKey:@"msg"] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alerView addAction:action];
    [self presentViewController:alerView animated:YES completion:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];


    self.title = @"设置资金密码";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    // 状态栏(statusbar)
//    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
//    
//    // 导航栏（navigationbar）
//    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    UILabel *setUpMoneyPasswordLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0+0+20, UISCREEN_WIDTH, 40)];
    setUpMoneyPasswordLabel.text = @"      当前密码";
    setUpMoneyPasswordLabel.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:setUpMoneyPasswordLabel];
    
    _setUpMoneyPasswordTextField = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(setUpMoneyPasswordLabel.frame)+5, UISCREEN_WIDTH-120, 30)];
    _setUpMoneyPasswordTextField.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _setUpMoneyPasswordTextField.clearButtonMode = YES; //清除模式开启
    _setUpMoneyPasswordTextField.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _setUpMoneyPasswordTextField.delegate = self;
    [self.view addSubview:_setUpMoneyPasswordTextField];

    
    UILabel *setUpMoneyPasswordLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(setUpMoneyPasswordLabel.frame)+1, UISCREEN_WIDTH, 40)];
    setUpMoneyPasswordLabel2.text = @"      新密码";
    setUpMoneyPasswordLabel2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:setUpMoneyPasswordLabel2];
    
    _setUpMoneyPasswordTextField2 = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(setUpMoneyPasswordLabel2.frame)+5, UISCREEN_WIDTH-120, 30)];
    _setUpMoneyPasswordTextField2.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _setUpMoneyPasswordTextField2.clearButtonMode = YES; //清除模式开启
    _setUpMoneyPasswordTextField2.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _setUpMoneyPasswordTextField2.delegate = self;
    [self.view addSubview:_setUpMoneyPasswordTextField2];
    
    UILabel *setUpMoneyPasswordLabel3 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(setUpMoneyPasswordLabel2.frame)+1, UISCREEN_WIDTH, 40)];
    setUpMoneyPasswordLabel3.text = @"      确认新密码";
    setUpMoneyPasswordLabel3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:setUpMoneyPasswordLabel3];

    _setUpMoneyPasswordTextField3 = [[UITextField alloc]initWithFrame:CGRectMake(120, CGRectGetMinY(setUpMoneyPasswordLabel3.frame)+5, UISCREEN_WIDTH-120, 30)];
    _setUpMoneyPasswordTextField3.returnKeyType = UIReturnKeyDone;  //返回按钮的样式
//    _setUpMoneyPasswordTextField3.clearButtonMode = YES; //清除模式开启
    _setUpMoneyPasswordTextField3.secureTextEntry = YES; //密码输入框
    //tf.keyboardType = UIKeyboardTypeNumberPad; //键盘类型
    //协议执行者交给textField自己self
    _setUpMoneyPasswordTextField3.delegate = self;
    [self.view addSubview:_setUpMoneyPasswordTextField3];

    UILabel *setUpMoneyPasswordLabel4 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(setUpMoneyPasswordLabel3.frame)+1, UISCREEN_WIDTH, 40)];
    setUpMoneyPasswordLabel4.text = @"        密码包括6-16字母和数字，不能和登录密码相同";
    setUpMoneyPasswordLabel4.textColor = [UIColor grayColor];
    setUpMoneyPasswordLabel4.backgroundColor = [UIColor clearColor];
    setUpMoneyPasswordLabel4.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:setUpMoneyPasswordLabel4];
    
    UIButton *setUpMoneyPasswordButton = [[UIButton alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(setUpMoneyPasswordLabel4.frame)+1, UISCREEN_WIDTH-20-20, 40)];
    [setUpMoneyPasswordButton setTitle:@"设置" forState:UIControlStateNormal];
    [setUpMoneyPasswordButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    setUpMoneyPasswordButton.backgroundColor = [UIColor colorWithRed:35/255.0f green:11/255.0f blue:53/255.0f alpha:1];
    [setUpMoneyPasswordButton addTarget:self action:@selector(supbAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:setUpMoneyPasswordButton];

}

// 设置按钮点击事件
- (void)supbAction:(UIButton *)sender {
    [self obtainData];
}

//UITextField控件协议方法：当点击返回按钮时方法执行
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
